import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:nyh_motorcade_app/pages/transaction_flow/item/transaction_flow_item.dart';
import 'package:nyh_motorcade_app/styles/colors.dart';
import 'package:nyh_motorcade_app/widgets/multi_state/multi_state_widget.dart';
import 'package:nyh_motorcade_app/widgets/search_app_bar.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'search_transaction_flow_controller.dart';

class SearchTransactionFlowPage extends StatelessWidget {
  final controller = Get.put(SearchTransactionFlowController());

  @override
  Widget build(BuildContext context) {
    return GetBuilder<SearchTransactionFlowController>(
      builder: (controller) {
        return Scaffold(
          backgroundColor: Colours.color_background,
          resizeToAvoidBottomInset: false,
          appBar: SearchAppBar(
            controller: controller.searchController,
            focusNode: controller.searchFocusNode,
            context: context,
            hintText: "请输入车牌号/司机姓名",
            onChanged: (value) {
              controller.searchTransactionFlow(value);
            },
          ),
          body: Container(
            child: Stack(
              fit: StackFit.expand,
              alignment: Alignment.center,
              children: [buildHistoryRecordWidget(), buildSearchResultWidget()],
            ),
          ),
        );
      },
    );
  }

  Widget buildHistoryRecordWidget() {
    return Visibility(
      visible: controller.keyword.isEmpty,
      child: Column(
        children: [
          Container(
            height: 40.w,
            padding: EdgeInsets.only(left: 16.w),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text(
                  "历史搜索",
                  style: TextStyle(
                    color: Colours.color_text_theme,
                    fontSize: 14.sp,
                    fontWeight: FontWeight.bold,
                  ),
                ),
                Visibility(
                  visible: controller.historyRecordList.isNotEmpty,
                  child: IconButton(
                    onPressed: () {
                      controller.deleteHistoryRecord();
                    },
                    icon: Image.asset("images/ic_delete.png"),
                  ),
                ),
              ],
            ),
          ),
          Expanded(
            child: ListView.separated(
              shrinkWrap: true,
              physics: BouncingScrollPhysics(),
              itemBuilder: (context, index) {
                return buildHistoryRecordItemWidget(index);
              },
              separatorBuilder: (context, index) {
                return Divider(color: Colours.color_background, height: 1.w, thickness: 1.w);
              },
              itemCount: controller.historyRecordList.length,
            ),
          ),
        ],
      ),
    );
  }

  Widget buildHistoryRecordItemWidget(int index) {
    String string = controller.historyRecordList[index];
    return Material(
      child: InkWell(
        onTap: () {
          controller.searchController.text = string;
          controller.searchTransactionFlow(string);
        },
        child: Padding(
          padding: EdgeInsets.symmetric(vertical: 17.w, horizontal: 16.w),
          child: Text(
            string,
            style: TextStyle(color: Colours.color_text_theme, fontSize: 14.sp),
            maxLines: 1,
            overflow: TextOverflow.ellipsis,
          ),
        ),
      ),
    );
  }

  Widget buildSearchResultWidget() {
    return Visibility(
      visible: controller.keyword.isNotEmpty,
      child: MultiStateWidget(
        child: Column(
          children: [
            Container(
              height: 40.w,
              padding: EdgeInsets.only(left: 16.w),
              child: Row(
                children: [
                  Text(
                    "相关交易流水(${controller.orderCount})",
                    style: TextStyle(
                      color: Colours.color_text_theme,
                      fontSize: 14.sp,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                ],
              ),
            ),
            Expanded(
              child: SmartRefresher(
                controller: controller.refreshController,
                enablePullDown: false,
                enablePullUp: true,
                onLoading: controller.onLoading,
                child: ListView.builder(
                  shrinkWrap: true,
                  physics: BouncingScrollPhysics(),
                  itemCount: controller.transactionFlowList.length,
                  itemBuilder: (context, index) {
                    return TransactionFlowItem(
                      controller.transactionFlowList[index],
                      focusNode: controller.searchFocusNode,
                    );
                  },
                ),
              ),
            ),
          ],
        ),
        state: controller.multiState,
      ),
    );
  }
}
